import java.util.ArrayList;
import java.util.Scanner;

public class newcode1 {
    private static ArrayList<FinancialRecord> incomeRecords = new ArrayList<>();
    private static ArrayList<FinancialRecord> expenseRecords = new ArrayList<>();

    public static void recordIncome(String date, double amount, String category, String note) {
        if (amount > 0) {
            incomeRecords.add(new FinancialRecord(date, amount, category, note));
            System.out.println("Income recorded successfully.");
        } else {
            System.out.println("Income amount must be positive.");//下一个

        }
    }

    public static void recordExpense(String date, double amount, String category, String note) {
        if (amount > 0) {
            expenseRecords.add(new FinancialRecord(date, amount, category, note));
            System.out.println("Expense recorded successfully.");
        } else {
            System.out.println("Expense amount must be positive.");
        }
    }

    // 查看所有账单
    public static void viewAllBills() {
        System.out.println("Income Records:");
        for (FinancialRecord income : incomeRecords) {
            System.out.println(income);
        }

        System.out.println("Expense Records:");
        for (FinancialRecord expense : expenseRecords) {
            System.out.println(expense);
        }
    }

    // 查询某日账单（收入 - 支出）
    public static void checkBillByDate(String date) {
        double totalIncome = 0;
        double totalExpense = 0;

        for (FinancialRecord income : incomeRecords) {
            if (income.date.equals(date)) {
                totalIncome += income.amount;
            }
        }

        for (FinancialRecord expense : expenseRecords) {
            if (expense.date.equals(date)) {
                totalExpense += expense.amount;
            }
        }

        double netAmount = totalIncome - totalExpense;
        System.out.println("On " + date + ": Net Amount = " + netAmount + " (Income: " + totalIncome + " - Expense: " + totalExpense + ")");
    }

    // 预算管理：计算总收入 - 总支出
    public static void budgetManagement() {
        double totalIncome = 0;
        double totalExpense = 0;

        for (FinancialRecord income : incomeRecords) {
            totalIncome += income.amount;
        }

        for (FinancialRecord expense : expenseRecords) {
            totalExpense += expense.amount;
        }

        double budget = totalIncome - totalExpense;
        System.out.println("Current Budget: " + budget);
    }

    // 月度统计：计算一个月的总收入和总支出
    public static void monthlySummary(String month) {
        double monthlyIncome = 0;
        double monthlyExpense = 0;

        for (FinancialRecord income : incomeRecords) {
            if (income.date.startsWith(month)) {  // 例如 "2024-09"
                monthlyIncome += income.amount;
            }
        }

        for (FinancialRecord expense : expenseRecords) {
            if (expense.date.startsWith(month)) {
                monthlyExpense += expense.amount;
            }
        }

        System.out.println("For " + month + ": Income = " + monthlyIncome + ", Expense = " + monthlyExpense);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Choose an option: " + "\n" + "1. Record Income  " + "\n" + "2. Record Expense" +
                    "\n" + "3. View All Bills" + "\n" + "4. Check Bill by Date" + "\n" + "5. Budget Management" +
                    "\n" + "6. Monthly Summary" + "\n" + "7. Exit");
            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume some newline

            if (choice == 1 || choice == 2) {
                System.out.print("Enter date (YYYY-MM-DD): ");
                String date = scanner.nextLine();

                System.out.print("Enter amount: ");
                double amount = scanner.nextDouble();
                scanner.nextLine();  // Consume newline

                System.out.print("Enter category: ");
                String category = scanner.nextLine();

                System.out.print("Enter note: ");
                String note = scanner.nextLine();

                if (choice == 1) {
                    recordIncome(date, amount, category, note);
                } else if (choice == 2) {
                    recordExpense(date, amount, category, note);
                }
            } else if (choice == 3) {
                viewAllBills();
            } else if (choice == 4) {
                System.out.print("Enter date (YYYY-MM-DD) to check: ");
                String date = scanner.nextLine();
                checkBillByDate(date);
            } else if (choice == 5) {
                budgetManagement();
            } else if (choice == 6) {
                System.out.print("Enter month (YYYY-MM) for summary: ");
                String month = scanner.nextLine();
                monthlySummary(month);
            } else if (choice == 7) {
                break;
            } else {
                System.out.println("Invalid choice. Please try again.");
            }
        }

        scanner.close();
    }
}

class FinancialRecord {
    String date;
    double amount;
    String category;
    String note;

    public FinancialRecord(String date, double amount, String category, String note) {
        this.date = date;
        this.amount = amount;
        this.category = category;
        this.note = note;
    }

    @Override
    public String toString() {
        return "Date: " + date + ", Amount: " + amount + ", Category: " + category + ", Note: " + note;
    }
}
